package com.technology.education.ui.student.deposit

import android.view.View
import android.widget.LinearLayout
import com.angcyo.dsladapter.*
import com.technology.education.R
import com.technology.education.base.BaseRecyclerTopBarFragment
import com.technology.education.data.SpKeys
import com.technology.education.data.model.ChargePackModel
import com.technology.education.dslitem.ItemSingleButton
import com.technology.education.ext.*
import me.hgj.jetpackmvvm.ext.parseState
import me.hgj.jetpackmvvm.ext.view.afterTextChange

/**
 * 学员 充值
 */
class DepositFragment : BaseRecyclerTopBarFragment<DepositViewModel>() {
    override fun initViews() {
        setTitle("充值")
        recycler.setBackgroundColor(getColor(R.color.bg_activity_gray))
        initGrid(2)
    }

    override fun lazyLoadData() {
        loading()
        mViewModel.getData()
    }

    private fun setUI(list: List<ChargePackModel>) {
        renderAdapter {

            resetItem(list.toDslItemList(R.layout.item_deposit) {
                itemTopInsert = 16 * dpi
                itemLeftInsert = 16 * dpi
                itemSpanCount = 1
                itemBindOverride = { itemHolder, itemPos, _, _ ->
                    itemRightInsert = if (itemPos % 2 == 1) 16 * dpi else 0
                    itemHolder.v<LinearLayout>(R.id.ll_parent)?.apply {
                        background =
                            getResId(if (mViewModel.selectPos == itemPos) R.drawable.bg_button else R.drawable.bg_round_white)
                    }
                    itemData?.let {
                        if (it is ChargePackModel) {
                            itemHolder.tv(R.id.tv_time)?.apply {
                                text = "${it.duration} h"
                                setTextColor(getColor(if (mViewModel.selectPos == itemPos) R.color.white else R.color.text_h))
                            }
                            itemHolder.tv(R.id.tv_money)?.apply {
                                text = "¥${it.money}"
                                setTextColor(getColor(if (mViewModel.selectPos == itemPos) R.color.white else R.color.text_h))
                            }
                            itemHolder.clickItem {_->
                                itemData?.apply {
                                    toggleCustom(false, -1)
                                    mViewModel.money = (this as ChargePackModel).duration.toString()
                                    mViewModel.packageId=it.id
                                    mViewModel.selectPos = itemPos
                                    updateAllItem()
                                }
                            }
                        }
                    }
                }
            })

            dslItem(R.layout.item_deposit) {
                itemTopInsert = 16 * dpi
                itemLeftInsert = 16 * dpi
                itemSpanCount = 1
                itemTag = "custom"
                itemBindOverride = { itemHolder, itemPos, _, _ ->
                    itemHolder.v<LinearLayout>(R.id.ll_parent)?.apply {
                        background =
                            getResId(if (mViewModel.selectPos == itemPos) R.drawable.bg_button else R.drawable.bg_round_white)
                    }
                    itemRightInsert = if (itemPos % 2 == 1) 16 * dpi else 0
                    itemHolder.tv(R.id.tv_time)?.apply {
                        text = "自定义"
                        setTextColor(getColor(if (mViewModel.selectPos == itemPos) R.color.white else R.color.text_h))
                    }
                    itemHolder.tv(R.id.tv_money)?.visibility = View.GONE
                    itemHolder.clickItem {
                        toggleCustom(true, itemPos)
                        mViewModel.selectPos = itemPos
                        updateAllItem()
                    }
                }
            }

            dslItem(R.layout.item_deposit_rule) {
                itemSpanCount = -1
                itemTopInsert = 8 * dpi
            }
            dslItem(ItemSingleButton("立即充值")) {
                style.topPadding = 16
                style.itemBackColor = R.color.bg_activity_gray
                itemClick = {
                    mViewModel.commitOrder()
                }
            }
        }
    }

    private fun toggleCustom(b: Boolean, pos: Int) {
        renderAdapter {
            if (b) {
                if (findItemByTag("input") == null) {
                    mViewModel.money = ""
                    mViewModel.packageId=""
                    insertItem(pos + 1, DslAdapterItem().apply {
                        itemTag = "input"
                        itemLayoutId = R.layout.item_deposit_input
                        itemSpanCount = -1
                        itemTopInsert = 16 * dpi
                        itemLeftInsert = 12 * dpi
                        itemRightInsert = 12 * dpi
                        itemBindOverride = { itemHolder, _, _, _ ->
                            itemHolder.et(R.id.et_content)?.apply {
                                afterTextChange {
                                    mViewModel.money = it
                                }
                            }
                        }
                    }, checkExist = true)
                }

            } else {
                mViewModel.money = ""
                removeItem("input")
            }
        }
    }

    override fun createObserver() {
        mViewModel.result.observe(this) {
            parseState(it, onSuccess = { list ->
                setUI(list)
            }, onError = {
                loadError()
            })
        }

        mViewModel.chargeResult.observe(this){ it ->
            parseState(it,onSuccess = {
                toPayActivity(mViewModel.money,it.id,SpKeys.PAY_TYPE_CHARGE)
            },onError = {
                toast(it.errorMsg)
            })

        }
        observe(SpKeys.PAY_RESULT_SUCCESS,String::class.java){
            popTo(recycler,R.id.main_fragment)
        }
    }
}